#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <memory>
#include <thread>
#include "ConnectionPool.h"
using namespace std;
//1.单线程：使用/不使用连接池
//2.多线程：使用/不适用连接池
void op1(int begin,int end)
{
    for(int i = begin;i < end; ++i)
    {
        MysqlConn conn;
        conn.connect("root","12345679","testdb","127.0.0.1");
        char sql[1024] = {0};
        sprintf(sql,"insert into people values(%d,25,'man','tom')",i);
        conn.update(sql);
    }
}
void op2(ConnectionPool* pool,int begin,int end)
{
    for(int i = begin;i < end; ++i)
    {
        shared_ptr<MysqlConn> conn = pool->getConnetion();
        char sql[1024] = {0};
        sprintf(sql,"insert into people values(%d,25,'man','tom')",i);
        conn->update(sql);
    }
}
void test1()
{
#if 0
//非连接池，单线程，用时：51800608573纳秒，51800毫秒
    steady_clock::time_point begin = steady_clock::now();
    op1(0,5000);
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "非连接池，单线程，用时：" << length.count() << "纳秒，"
    << length.count() / 1000000 << "毫秒" << endl;
#else
//连接池，单线程，用时：24450335507纳秒，24450毫秒
    ConnectionPool* pool = ConnectionPool::getConnetPool(); 
    steady_clock::time_point begin = steady_clock::now();
    op2(pool,0,5000);
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "连接池，单线程，用时：" << length.count() << "纳秒，"
    << length.count() / 1000000 << "毫秒" << endl;
#endif    
}

void test2()
{
#if 0
//非连接池，多线程，用时：18366204749纳秒，18366毫秒
    MysqlConn conn;
    conn.connect("root","12345679","testdb","127.0.0.1");
    steady_clock::time_point begin = steady_clock::now();
    thread t1(op1,0,1000);
    thread t2(op1,1000,2000);
    thread t3(op1,2000,3000);
    thread t4(op1,3000,4000);
    thread t5(op1,4000,5000);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "非连接池，多线程，用时：" << length.count() << "纳秒，"
    << length.count() / 1000000 << "毫秒" << endl;
#else
//连接池，多线程，用时：6621424734纳秒，6621毫秒
    ConnectionPool* pool = ConnectionPool::getConnetPool(); 
    steady_clock::time_point begin = steady_clock::now();
    thread t1(op2,pool,0,1000);
    thread t2(op2,pool,1000,2000);
    thread t3(op2,pool,2000,3000);
    thread t4(op2,pool,3000,4000);
    thread t5(op2,pool,4000,5000);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "连接池，多线程，用时：" << length.count() << "纳秒，"
    << length.count() / 1000000 << "毫秒" << endl;
#endif
}
int query()
{
    //插入一条数据
    MysqlConn conn;
    conn.connect("root","12345679","testdb","127.0.0.1");
    string sql = "insert into people values(5,25,'man','curry')";
    bool flag = conn.update(sql);
    cout <<"flag value: " << flag << endl;
    sql = "select * from people";
    conn.query(sql);
    while (conn.next())
    {
        cout << conn.value(0) << ","
             << conn.value(1) << ","
             << conn.value(2) << ","
             << conn.value(3) << endl;
    }
    return 0;
}
int main()
{
    //test1();
    test2();
    return 0;
}